package com.example.petshopping.ui;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.petshopping.R;
import com.example.petshopping.adapter.FishTankProductAdapter;
import com.example.petshopping.model.Product;
import com.google.android.material.tabs.TabLayout;

import java.util.ArrayList;
import java.util.List;

/**
 * 鱼缸造景悬赏页面活动类
 */
public class FishTankLandscapingRewardActivity extends AppCompatActivity {

    private RecyclerView recyclerProducts;
    private FishTankProductAdapter adapter;
    private List<Product> productList;
    private TabLayout tabLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fish_tank_landscaping_reward);

        initViews();
        initData();
        setupRecyclerView();
        setupListeners();
    }

    private void initViews() {
        recyclerProducts = findViewById(R.id.recycler_products);
        tabLayout = findViewById(R.id.tab_layout);
    }

    private void initData() {
        productList = new ArrayList<>();
        
        // 添加鱼缸造景商品数据
//        productList.add(new Product("1", "天然火山石鱼缸造景石头", "yugang", "¥29.9", "海底世界","", null));
//        productList.add(new Product("2", "仿真水草鱼缸装饰水草", "yugang", "¥19.9", "水族之家","", null));
//        productList.add(new Product("3", "水族箱专用彩色底砂", "yugang", "¥15.8", "鱼乐水族","", null));
//        productList.add(new Product("4", "仿古沉船鱼缸装饰品", "yugang", "¥39.9", "海洋水族","", null));
    }

    private void setupRecyclerView() {
        // 设置网格布局，2列
        GridLayoutManager layoutManager = new GridLayoutManager(this, 2);
        recyclerProducts.setLayoutManager(layoutManager);
        
        // 设置适配器
        adapter = new FishTankProductAdapter(this, productList);
        recyclerProducts.setAdapter(adapter);
    }

    private void setupListeners() {
        // 设置点击事件
        adapter.setOnItemClickListener(new FishTankProductAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(Product product, int position) {
                // 点击商品项，跳转到详情页
                Toast.makeText(FishTankLandscapingRewardActivity.this, 
                        "点击了: " + product.getName(), Toast.LENGTH_SHORT).show();
                // 这里可以添加跳转到商品详情页的代码
                // Intent intent = new Intent(FishTankLandscapingRewardActivity.this, ProductDetailActivity.class);
                // intent.putExtra("product_id", product.getId());
                // startActivity(intent);
            }

            @Override
            public void onCloseClick(Product product, int position) {
                // 点击关闭按钮，移除商品
                Toast.makeText(FishTankLandscapingRewardActivity.this, 
                        "移除了: " + product.getName(), Toast.LENGTH_SHORT).show();
                productList.remove(position);
                adapter.notifyItemRemoved(position);
            }
        });

        // 设置标签页选择监听器
        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                int position = tab.getPosition();
                switch (position) {
                    case 0:
                        // 悬赏页面（当前页面）
                        break;
                    case 1:
                        // 购买页面 - 跳转到购买页面
                        Intent intent = new Intent(FishTankLandscapingRewardActivity.this, 
                                FishTankLandscapingPurchaseActivity.class);
                        startActivity(intent);
                        finish(); // 结束当前页面
                        break;
                }
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                // 不需要处理
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {
                // 不需要处理
            }
        });
    }
} 